
import csv
import pymysql

file_path = 'datasets/data15.csv'

all_data = []
with open(file_path, mode='r', encoding='utf-8') as file:
    csv_reader = csv.reader(file)

    # 跳过标题头部（如果CSV有标题行）
    next(csv_reader)

    # 逐行读取csv文件
    for row in csv_reader:
        print(row)  # row是一个列表，包含了当前行的所有字段
        all_data.append(row)

import pymysql

# 数据库连接信息
connect_info = {
    'host': 'localhost',
    'user': 'root',
    'password': '123456',
    'db': 'guangfu',
    'charset': 'utf8mb4'
}

# 连接到数据库
connection = pymysql.connect(**connect_info)

try:
    with connection.cursor() as cursor:
        # 编写 SQL 插入语句，字段数量要与列表中子列表的长度匹配
        sql = "INSERT INTO `data_table` (`date_d`, `one`, `two`, `three`, `four`, `wu`, `liu`, `qi`, `ba`, `jiu`, `shi`, `shiyi`, `shier`, `shisan`, `shisi`, `shiwu`) VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s)"
        # 批量执行 SQL 语句
        cursor.executemany(sql, all_data)

    # 提交事务
    connection.commit()

except pymysql.MySQLError as e:
    print(f"Error: {e}")

finally:
    connection.close()

